package com.luck.easyfoodbatch.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.luck.easyfoodbatch.entity.Classification;
import com.luck.easyfoodbatch.mvp.model.ClassModel;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lmw
 * @ClassName: FindDB
 * @Description: 数据库查询类
 * @date 2014-12-2 下午1:42:15
 */
public class ClassDB {
    private static SQLiteDatabase database;
    private DBManager dBManager;

    public ClassDB(Context context) {
        dBManager = new DBManager(context);

    }

    /**
     * @return void 返回类型
     * @Title: openDataBase
     * @Description: 打开数据库
     * @author lmw
     */
    protected void openDataBase() {
        database = dBManager.openDatabase();
    }

    /**
     * @return void 返回类型
     * @Title: openDataBase
     * @Description: 关闭数据库
     * @author lmw
     */
    protected void closeDataBase() {
        if (null != database) {
            database.close();
        }
    }


    /**
     * 得到所有分类
     *
     * @return
     */
    public List<Classification> getByAllClassList() {
        if ((null == database) || !database.isOpen()) {
            openDataBase();
        }
        Cursor cur = database
                .rawQuery("SELECT * from classification", null);
        try {
            if (cur != null) {
                int num = cur.getCount();
                List<Classification> list = new ArrayList<Classification>(num);
                if (cur.moveToFirst()) {
                    do {
                        String cid = cur.getString(cur.getColumnIndex("cid"));
                        String type = cur.getString(cur.getColumnIndex("type"));
                        String name = cur.getString(cur.getColumnIndex("name"));
                        String img = cur.getString(cur.getColumnIndex("img"));
                        String img_base_path = cur.getString(cur.getColumnIndex("img_base_path"));
                        String sort = cur.getString(cur.getColumnIndex("sort"));
                        String state = cur.getString(cur.getColumnIndex("state"));
                        String created_at = cur.getString(cur.getColumnIndex("created_at"));
                        String updated_at = cur.getString(cur.getColumnIndex("updated_at"));
                        Classification message = new Classification();
                        message.setCid(cid);
                        message.setName(name);
                        message.setType(type);
                        message.setImg(img);
                        message.setImg_base_path(img_base_path);
                        message.setSort(sort);
                        message.setState(state);
                        message.setCreated_at(created_at);
                        message.setUpdated_at(updated_at);
                        list.add(message);
                    } while (cur.moveToNext());
                }
                return list;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            database.close();
            cur.close();
        }
        return null;
    }


    /**
     * 批量保存消息
     *
     * @return
     */
    public boolean saveClassList(List<ClassModel.DataBean.ImportBean> list) {
        if ((null == database) || !database.isOpen()) {
            openDataBase();
        }
        for (ClassModel.DataBean.ImportBean model : list) {
            if (!isClassModel(model.getCid())) {
                ContentValues values = new ContentValues();
                values.put("cid", model.getCid());
                values.put("type", model.getType());
                values.put("name", model.getName());
                values.put("img", model.getImg());
                values.put("img_base_path", model.getImg_base_path());
                values.put("sort", model.getSort());
                values.put("state", model.getState());
                values.put("created_at", model.getCreated_at());
                values.put("updated_at", model.getUpdated_at());
                database.insert("classification", null, values);
            }
        }
        database.close();
        return true;
    }


    /**
     * 判断消息是否存在
     *
     * @return
     */
    public boolean isClassModel(String cid) {
        if ((null == database) || !database.isOpen()) {
            openDataBase();
        }
        StringBuffer buffer = null;
        Cursor cur = database
                .rawQuery("SELECT * from classification where cid=" + cid + " order by created_at desc", null);
        try {
            if (cur != null) {
                if (cur.moveToFirst()) {
                    do {
                        return true;
                    } while (cur.moveToNext());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //database.close();
            cur.close();
        }
        return false;
    }


    /**
     * @return void 返回类型
     * @Title: deleteDataBase
     * @Description: 删除数据库
     * @author lmw
     */
    public void deleteDataBase(Context context) {
        context.deleteDatabase("area.db");
    }
}
